package com.ruoyi.project.xkx.school.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.project.xkx.school.domain.XkxSchoolCourse;
import com.ruoyi.project.xkx.school.service.IXkxSchoolCourseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.xkx.school.domain.XkxCourse;
import com.ruoyi.project.xkx.school.service.IXkxCourseService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 所授课程类型Controller
 *
 * @author ..
 * @date 2021-05-10
 */
@RestController
@RequestMapping("/school/course")
public class XkxCourseController extends BaseController {
  @Autowired
  private IXkxCourseService xkxCourseService;
  @Autowired
  private IXkxSchoolCourseService schoolCoirseService;

  @GetMapping("/courseAll")
  public AjaxResult getCourseAll(){
    return AjaxResult.success(xkxCourseService.list());
  }

  @GetMapping("/schoolCourse/{schoolId}")
  public AjaxResult getSchoolCourse(@PathVariable Integer schoolId){
    List<XkxSchoolCourse> school_id = schoolCoirseService.list(new QueryWrapper<XkxSchoolCourse>().eq("school_id", schoolId));
    return AjaxResult.success(school_id);
  }

  @PostMapping("/updateSchoolCourse/{schoolId}")
  public AjaxResult updateSchoolCourse(@PathVariable Integer schoolId,@RequestBody List<Integer> courseIds){
    boolean updateSchoolCourse = schoolCoirseService.updateSchoolCourse(schoolId, courseIds);

    if (updateSchoolCourse) {
      return AjaxResult.success();
    }else{
      return AjaxResult.error();
    }

  }

  /**
   * 查询所授课程类型列表
   */
  @PreAuthorize("@ss.hasPermi('school:course:list')")
  @GetMapping("/list")
  public TableDataInfo list(XkxCourse xkxCourse) {
    startPage();
    List<XkxCourse> list = xkxCourseService.selectXkxCourseList(xkxCourse);
    return getDataTable(list);
  }

  /**
   * 导出所授课程类型列表
   */
  @PreAuthorize("@ss.hasPermi('school:course:export')")
  @Log(title = "所授课程类型", businessType = BusinessType.EXPORT)
  @GetMapping("/export")
  public AjaxResult export(XkxCourse xkxCourse) {
    List<XkxCourse> list = xkxCourseService.selectXkxCourseList(xkxCourse);
    ExcelUtil<XkxCourse> util = new ExcelUtil<XkxCourse>(XkxCourse.class);
    return util.exportExcel(list, "所授课程类型数据");
  }

  /**
   * 获取所授课程类型详细信息
   */
  @PreAuthorize("@ss.hasPermi('school:course:query')")
  @GetMapping(value = "/{courseId}")
  public AjaxResult getInfo(@PathVariable("courseId") Integer courseId) {
    return AjaxResult.success(xkxCourseService.selectXkxCourseById(courseId));
  }

  /**
   * 新增所授课程类型
   */
  @PreAuthorize("@ss.hasPermi('school:course:add')")
  @Log(title = "所授课程类型", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@RequestBody XkxCourse xkxCourse) {
    return toAjax(xkxCourseService.insertXkxCourse(xkxCourse));
  }

  /**
   * 修改所授课程类型
   */
  @PreAuthorize("@ss.hasPermi('school:course:edit')")
  @Log(title = "所授课程类型", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@RequestBody XkxCourse xkxCourse) {
    return toAjax(xkxCourseService.updateXkxCourse(xkxCourse));
  }

  /**
   * 删除所授课程类型
   */
  @PreAuthorize("@ss.hasPermi('school:course:remove')")
  @Log(title = "所授课程类型", businessType = BusinessType.DELETE)
  @DeleteMapping("/{courseIds}")
  public AjaxResult remove(@PathVariable Integer[] courseIds) {
    return toAjax(xkxCourseService.deleteXkxCourseByIds(courseIds));
  }
}
